#include "dspSimulator/instruction/SPMASKR.h"

namespace dspsimulator {

    std::string SPMASKR::to_string() const {
        std::string str = m_name + " ";
        for (const auto &func_unit: m_masked_function_unit) {
            str += get_function_unit_name(func_unit) + " ";
        }
        return str;
    }

    std::shared_ptr<DspInstruction> SPMASKR::decode_standard_instruction(const std::shared_ptr<DspInstructionInfo> &dsp_instruction_info) {
        std::shared_ptr<SPMASKR> spmaskr;
        std::bitset<32> &data = dsp_instruction_info->m_standard_bits;
        if (common::check_bits<6>(data, 0b000000, 26) && common::check_bits<16>(data, 0b1100100000000000, 2)) {
            spmaskr = std::make_shared<SPMASKR>(dsp_instruction_info->address(), 4);
            bool L1 = data[18];
            if (L1) {
                spmaskr->m_masked_function_unit.emplace_back(FuncUnit::L1);
            }
            bool L2 = data[19];
            if (L2) {
                spmaskr->m_masked_function_unit.emplace_back(FuncUnit::L2);
            }
            bool S1 = data[20];
            if (S1) {
                spmaskr->m_masked_function_unit.emplace_back(FuncUnit::S1);
            }
            bool S2 = data[21];
            if (S2) {
                spmaskr->m_masked_function_unit.emplace_back(FuncUnit::S2);
            }
            bool D1 = data[22];
            if (D1) {
                spmaskr->m_masked_function_unit.emplace_back(FuncUnit::D1);
            }
            bool D2 = data[23];
            if (D2) {
                spmaskr->m_masked_function_unit.emplace_back(FuncUnit::D2);
            }
            bool M1 = data[24];
            if (M1) {
                spmaskr->m_masked_function_unit.emplace_back(FuncUnit::M1);
            }
            bool M2 = data[25];
            if (M2) {
                spmaskr->m_masked_function_unit.emplace_back(FuncUnit::M2);
            }
            spmaskr->m_p = data[0];
        }
        return spmaskr;
    }

    std::shared_ptr<DspInstruction> SPMASKR::decode_compact_instruction(const std::shared_ptr<DspInstructionInfo> &dsp_instruction_info) {
        return nullptr;
    }
}